package com.www.applyword.ui.my;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.EducationAdapter;
import com.www.applyword.adapter.SkillAdapter;
import com.www.applyword.adapter.WordHopeAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.ResumeBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2019/10/23 0023 15:47
 * version: 1.0
 */
public class MyReSumeActivity extends BaseActivity {

    @BindView(R.id.ll_evaluation)
    LinearLayout llEvaluation;
    @BindView(R.id.ll_job)
    LinearLayout llJob;
    @BindView(R.id.ll_word_experience)
    LinearLayout llWordExperience;
    @BindView(R.id.ll_education)
    LinearLayout llEducation;
    @BindView(R.id.ll_skills)
    LinearLayout llSkills;
    @BindView(R.id.ll_personal)
    LinearLayout ll_personal;
    @BindView(R.id.rl_person)
    RelativeLayout rl_person;
    @BindView(R.id.civ_head_photo)
    CircleImageView civHeadPhoto;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_appraise)
    TextView tvAppraise;

    @BindView(R.id.rl_word_hope)
    RecyclerView rlWordHope;
    @BindView(R.id.rl_word_time)
    RecyclerView rlWordTime;
    @BindView(R.id.rl_education)
    RecyclerView rlEducation;
    @BindView(R.id.rl_skill)
    RecyclerView rl_skill;
    @BindView(R.id.tv_job_type)
    TextView tvJobType;
    @BindView(R.id.tv_job_qiyu)
    TextView tvJobQiyu;
    @BindView(R.id.tv_hope_job)
    TextView tvHopeJob;
    @BindView(R.id.ll_job_type)
    LinearLayout llJobType;
    @BindView(R.id.ll_job_qiyu)
    LinearLayout llJobQiyu;
    @BindView(R.id.ll_hope_job)
    LinearLayout llHopeJob;
    @BindView(R.id.ll_Resume)
    LinearLayout ll_Resume;
    //编辑

    private ResumeBean.ResumeBasicBean resumeBasic;

    private WordHopeAdapter wordHopeAdapter;
    private EducationAdapter educationAdapter;
    private SkillAdapter skillAdapter;
    private List<ResumeBean.ResumeWorkBean> resumeWork = new ArrayList<>();
    private List<ResumeBean.ResumeSchoolBean> schoolBeans = new ArrayList<>();
    private List<ResumeBean.ResumeCertificateBean> certificateBeans = new ArrayList<>();


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.resume_activity);
        setTitle("我的简历");
        tv_right.setVisibility(View.VISIBLE);
        setRringTitle("预览");
        tv_right.setOnClickListener(v -> startActivity(new Intent(MyReSumeActivity.this, PersonalResumeActivity.class)));
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.DEL_RESUMESCHOOL)) {
                loadData();
            }
        }
    }

    @Override
    protected void loadData() {
        rlWordTime.setLayoutManager(new LinearLayoutManager(MyReSumeActivity.this));
        rlWordTime.setItemAnimator(new DefaultItemAnimator());
        wordHopeAdapter = new WordHopeAdapter(MyReSumeActivity.this);
        rlWordTime.setAdapter(wordHopeAdapter);
      //  rlWordTime.setHasFixedSize(true);
      //  rlWordTime.setNestedScrollingEnabled(false);

        rlEducation.setLayoutManager(new LinearLayoutManager(MyReSumeActivity.this));
        rlEducation.setItemAnimator(new DefaultItemAnimator());
        educationAdapter = new EducationAdapter(MyReSumeActivity.this);
        rlEducation.setAdapter(educationAdapter);
       // rlEducation.setHasFixedSize(true);
      //  rlEducation.setNestedScrollingEnabled(false);

        rl_skill.setLayoutManager(new LinearLayoutManager(MyReSumeActivity.this));
        rl_skill.setItemAnimator(new DefaultItemAnimator());
        skillAdapter = new SkillAdapter(MyReSumeActivity.this);
        rl_skill.setAdapter(skillAdapter);
     //   rl_skill.setHasFixedSize(true);
    //    rl_skill.setNestedScrollingEnabled(false);


        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(MyReSumeActivity.this, Constants.GETALLRESUME, params, new CallBack<ResumeBean>() {
            @Override
            public void onSuccess(ResumeBean result) {
                resumeBasic = result.getResumeBasic();
                String year1 = Utils.getYear(System.currentTimeMillis());
                String year2 = resumeBasic.getResumeBasic_DateOfBirth().substring(0, 4);
                int year3 = Integer.valueOf(year1) - Integer.valueOf(year2);
                //   tvName.setText(result.getResumeBasic().getResumeBasic_Name());
               if (!TextUtils.isEmpty(resumeBasic.getResumeBasic_City())) {
                    rl_person.setVisibility(View.VISIBLE);
                    ll_personal.setVisibility(View.GONE);
                    ImageUtil.setImage(civHeadPhoto, result.getResumeBasic().getResumeBasic_HeadImg());
                    tvName.setText( resumeBasic.getResumeBasic_UserName() + "\n"+ year3 + "岁" + "|" + resumeBasic.getResumeBasic_Sex() + "|" + resumeBasic.getResumeBasic_City() + "|" + resumeBasic.getResumeBasic_HighestDiploma()
                            + "|" + resumeBasic.getResumeBasic_EducationalStatus() + "|" + "身高:" + resumeBasic.getResumeBasic_Height() + "cm" + "\n"
                              + "手机号:" + resumeBasic.getResumeBasic_Mobile() );
                }

                if (!TextUtils.isEmpty(resumeBasic.getResumeBasic_Evaluate())) {

                    tvAppraise.setVisibility(View.VISIBLE);
                    tvAppraise.setText(resumeBasic.getResumeBasic_Evaluate());
                }

                if (!TextUtils.isEmpty(resumeBasic.getResumeBasic_WorkNature())){
                    llJobType.setVisibility(View.VISIBLE);
                    tvJobType.setText(resumeBasic.getResumeBasic_WorkNature()); //我想找长期稳定工作
                }
                if (!TextUtils.isEmpty(resumeBasic.getResumeBasic_Region())){
                    llJobQiyu.setVisibility(View.VISIBLE);
                    tvJobQiyu.setText(resumeBasic.getResumeBasic_Region());
                }
                if (!TextUtils.isEmpty(resumeBasic.getResumeBasic_PositionName())){
                    llHopeJob.setVisibility(View.VISIBLE);
                    tvHopeJob.setText(resumeBasic.getResumeBasic_PositionName());
                }

                //   tvLeisureTime.setText(resumeBasic.getResumeBasic_FreeTime());
                //  tvHopeMoney.setText(resumeBasic.getResumeBasic_SalaryExpectation());
                //  tvJobTime.setText(resumeBasic.getResumeBasic_WorkTime());

                resumeWork.clear();
                resumeWork.addAll(result.getResumeWork());
                wordHopeAdapter.replaceAll(resumeWork, true);

                schoolBeans.clear();
                schoolBeans.addAll(result.getResumeSchool());
                educationAdapter.replaceAll(schoolBeans, true);

                certificateBeans.clear();
                certificateBeans.addAll(result.getResumeCertificate());
                skillAdapter.replaceAll(certificateBeans, true);
            }
        });
    }


    @OnClick({R.id.ll_personal, R.id.ll_evaluation, R.id.ll_job, R.id.ll_word_experience, R.id.ll_education, R.id.ll_skills,R.id.ib_edit,R.id.ib_edit2,R.id.ll_Resume})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_personal:
                startActivity(new Intent(MyReSumeActivity.this, PersonalActivity.class));
                break;
            //自我评价
            case R.id.ll_evaluation:
                startActivity(new Intent(MyReSumeActivity.this, PersonalEvaluationActivity.class));
                break;
            case R.id.ll_job:
                startActivity(new Intent(MyReSumeActivity.this, JobExpectActivity.class));
                break;
            case R.id.ll_word_experience:
                startActivity(new Intent(MyReSumeActivity.this, JobActivity.class));
                break;
            //教育经历
            case R.id.ll_education:
                startActivity(new Intent(MyReSumeActivity.this, EvaluationActivity.class));
                break;
            case R.id.ll_skills:
                startActivity(new Intent(MyReSumeActivity.this, SkillsActivity.class));
                break;
                //编辑
            case R.id.ib_edit:
                startActivity(new Intent(MyReSumeActivity.this, PersonalActivity.class).putExtra("resumeBasic", resumeBasic));
                break;
                //工作期望
            case R.id.ib_edit2:
                startActivity(new Intent(MyReSumeActivity.this, JobExpectActivity.class).putExtra("resumeBasic", resumeBasic));
                break;
            case R.id.ll_Resume:
                startActivity(new Intent(MyReSumeActivity.this, ResumeWorkListActivity.class).putExtra("resumeBasic", resumeBasic));
                break;
            default:
        }
    }
}
